//
//  RoomScheduleView.m
//  mUBB
//
//  Created by Cosmin Stirbu on 3/16/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "RoomScheduleView.h"
#import "UIViewAdditions.h"
#import "NSDateAdditions.h"

@implementation RoomScheduleView

@synthesize delegate;

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        // BackgroundView
        _scheduleBackgroundView = [ScheduleBackgroundView new];
        _bookingClouds = [NSMutableArray new];
        // Scroll View
        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [scrollView addSubview:_scheduleBackgroundView];
        scrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin |UIViewAutoresizingFlexibleBottomMargin;
        scrollView.contentSize = CGSizeMake(_scheduleBackgroundView.width, _scheduleBackgroundView.height);
        [self addSubview:scrollView];
    }
    return self;
}

- (void)displayBookings:(NSArray *)bookings {
    _bookigns = bookings;
    [_bookingClouds makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [_bookingClouds removeAllObjects];
    
    for (Booking *booking in bookings) {
        BookingCloud *bookingCloud = [[BookingCloud alloc] initWithBooking:booking];
        bookingCloud.top = ([booking.startDate hoursAndMinutesAsFloat] - 7) * VERTICAL_DIFF + VERTICAL_OFFSET + FONT_SIZE / 2;
        bookingCloud.left = HORIZONTAL_OFFSET + 2 * HORIZONTAL_DIFFERENCE + TIME_WIDTH;
        bookingCloud.height = [NSDate differenceBetween:booking.startDate andDate:booking.endDate] * VERTICAL_DIFF;
        [_scheduleBackgroundView addSubview:bookingCloud];
        [_bookingClouds addObject:bookingCloud];
        [bookingCloud addTarget:delegate action:@selector(didSelectBookingCloud:) forControlEvents:UIControlEventTouchUpInside];
    }
    [scrollView setContentOffset:CGPointMake(0,0) animated:NO];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
